package com.ueboot.weishop.admin.controller.setting;

import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.admin.controller.setting.vo.*;
import com.ueboot.weishop.entity.setting.*;
import com.ueboot.weishop.service.setting.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类管理
 */
@Slf4j
@RestController
@RequestMapping(value = "/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private AttributeService attributeService;

    @Autowired
    private AttrOptionValueService attrOptionValueService;

    @Autowired
    private SpecificationService specificationService;

    @Autowired
    private SpecOptionValueService specOptionValueService;

    @RequiresPermissions("category:read")
    @RequestMapping(value = "/find")
    public Response<List<CategoryResp>> find() {
        List<CategoryEntity> result = categoryService.findAll();
        List<CategoryResp> list = new ArrayList<>();
        result.forEach(item -> {
            CategoryResp resp = new CategoryResp();
            BeanUtils.copyProperties(item, resp);
            if (item.getParent() != null) {
                resp.setParentId(item.getParent().getId());
            }
            resp.setValue(item.getId());
            list.add(resp);
        });
        return new Response<>(list);
    }


    @RequiresPermissions("category:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody CategoryReq req) {
        CategoryEntity entity = null;
        if (req.getId() == null) {
            entity = new CategoryEntity();
        } else {
            entity = categoryService.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);

        if (req.getParentId() != null) {
            CategoryEntity parent = categoryService.get(req.getParentId());
            entity.setParent(parent);

            entity.setFullPathName(parent == null || parent.getFullPathName() == null ?  null : parent.getFullPathName() + parent.getName() + "/");
            entity.setFullPathId(parent == null || parent.getFullPathId() == null ? null : parent.getFullPathId() + parent.getId() + "/");

        }

        categoryService.save(entity);

        return new Response<>();
    }

    @RequiresPermissions("category:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] ids) {
        categoryService.delete(ids);
        return new Response<>();
    }

    @RequiresPermissions("category:read")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Response<CategoryResp> get(@PathVariable Long id) {
        CategoryEntity entity = categoryService.get(id);
        CategoryResp resp = new CategoryResp();
        BeanUtils.copyProperties(entity, resp);
        return new Response<>(resp);
    }

    /**
     * 根据分类查询分类下的品牌、属性、规格
     * @param id
     * @return
     */
    @RequiresPermissions("category:read")
    @RequestMapping(value = "/findByCategoryId/{id}", method = RequestMethod.GET)
    public Response<CategoryFindResp> findByCategoryId(@PathVariable Long id) {
        CategoryFindResp result = new CategoryFindResp();
        //品牌列表
        List<BrandEntity> brandEntityList = brandService.findByCategoryId(id);
        List<BrandFindResp> brandFindRespList = new ArrayList<>();
        brandEntityList.forEach((brand) -> {
            BrandFindResp resp = new BrandFindResp();
            BeanUtils.copyProperties(brand, resp);
            resp.setLabel(brand.getName());
            resp.setValue(brand.getId());
            resp.setName(brand.getName());
            brandFindRespList.add(resp);
        });
        result.setBrands(brandFindRespList);

        //属性列表
        List<AttributeEntity> attributeEntityList = attributeService.findByCategoryId(id);
        List<AttributeResp> attributeRespList = new ArrayList<>();
        attributeEntityList.forEach((attributeEntity -> {
            AttributeResp resp = new AttributeResp();
            BeanUtils.copyProperties(attributeEntity, resp);
            //查询属性对应的属性可选值列表
            List<AttrOptionValueEntity> attrOptionValueEntities = attrOptionValueService.findByAttrId(attributeEntity.getId());
            List<AttrOptionValueResp> attrOptionValueRespList = new ArrayList<>();
            attrOptionValueEntities.forEach((entity) -> {
                AttrOptionValueResp resp1 = new AttrOptionValueResp();
                BeanUtils.copyProperties(entity, resp1);
                attrOptionValueRespList.add(resp1);
            });
            resp.setOptions(attrOptionValueRespList);
            attributeRespList.add(resp);
        }));
        result.setAttrs(attributeRespList);

        //规格列表
        List<SpecificationEntity> specificationEntities = specificationService.findByCategoryId(id);
        List<SpecificationResp> specificationRespList = new ArrayList<>();
        specificationEntities.forEach((entity) -> {
            SpecificationResp resp = new SpecificationResp();
            BeanUtils.copyProperties(entity, resp);
            //规格对应的规格可选值列表
            List<SpecOptionValueEntity> specOptionValueEntities = specOptionValueService.findBySpecId(entity.getId());
            List<SpecOptionValueResp> specOptionValueResps = new ArrayList<>();
            specOptionValueEntities.forEach((entity1) -> {
                SpecOptionValueResp resp1 = new SpecOptionValueResp();
                BeanUtils.copyProperties(entity1, resp1);
                specOptionValueResps.add(resp1);
            });
            resp.setOptions(specOptionValueResps);
            specificationRespList.add(resp);
        });
        result.setSpecs(specificationRespList);
        return new Response<>(result);
    }

}
